/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.cef.generator.repository.dac.method;

import com.inspur.edp.caf.generator.baseInfo.ParameterInfo;
import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.caf.generator.method.ClassMethodGenerator;
import com.inspur.edp.cef.entity.entity.ICefData;
import com.inspur.edp.cef.gencommon.MediateType;
import com.inspur.edp.cef.generator.coreGenerator.entityGenerator.CommonModelCoreGeneratorContext;
import com.inspur.edp.cef.generator.repository.dac.EntityDacGenContext;
import com.inspur.edp.cef.repository.dac.ChildEntityDac;
import org.eclipse.jdt.core.dom.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class CreateInstanceMethodGenerator extends ClassMethodGenerator {

  private EntityDacGenContext dacContext;
  private String nodeCode;
  private boolean isChild;

  public CreateInstanceMethodGenerator(EntityDacGenContext dacContext,String nodeCode, boolean isChild)
  {
    this.dacContext = dacContext;
    this.nodeCode = nodeCode;
    this.isChild = isChild;
  }

  @Override
  protected String getMethodName() {
    return "createInstance";
  }

  @Override
  protected TypeInfo getReturnType() {
    TypeInfo typeInfo = new TypeInfo(ICefData.class.getTypeName());
    return typeInfo;
  }

  @Override
  protected boolean getIsOverride() {
    return true;
  }

  @Override
  protected ArrayList<Modifier.ModifierKeyword> getAccessModifiers() {
    ArrayList<Modifier.ModifierKeyword> list = new ArrayList<>();
    list.add(Modifier.ModifierKeyword.PUBLIC_KEYWORD);
    return list;
  }

  @Override
  protected ArrayList<ParameterInfo> getParameterCollection() {
    return null;
  }

  protected String buildEntityTypeName()
  {
    if (isChild)
    {
      java.util.HashMap<String, MediateType> childTypes = ((CommonModelCoreGeneratorContext) dacContext.getReposContext().getCefContext().getCefGenContext().getCoreGenContext()).getEntityDataMediateTypes();
      if (!childTypes.containsKey(nodeCode))
      {
        throw new RuntimeException("不存在编号为" + nodeCode + "的子表");
      }
      return childTypes.get(nodeCode).getClassName();
    }
    return ((CommonModelCoreGeneratorContext) dacContext.getReposContext().getCefContext().getCefGenContext().getCoreGenContext()).getEntityDataMediateTypes().get(nodeCode).getClassName();
  }

//  SaleOrderTTData entity = new SaleOrderTTData();
//	    return entity;
  @Override
  protected Block buildMethodBody() {
    Block block = ast.newBlock();
    VariableDeclarationFragment variableDeclarationFragment = ast.newVariableDeclarationFragment();
    variableDeclarationFragment.setName(ast.newSimpleName("entity"));
    ClassInstanceCreation classInstanceCreation = ast.newClassInstanceCreation();
    classInstanceCreation.setType(ast.newSimpleType(ast.newSimpleName(buildEntityTypeName())));
    variableDeclarationFragment.setInitializer(classInstanceCreation);

    VariableDeclarationStatement variableDeclarationStatement = ast.newVariableDeclarationStatement(variableDeclarationFragment);
    variableDeclarationStatement.setType(ast.newSimpleType(ast.newSimpleName(buildEntityTypeName())));
    block.statements().add(variableDeclarationStatement);

    ReturnStatement returnStatement = ast.newReturnStatement();
    returnStatement.setExpression(ast.newSimpleName("entity"));
    block.statements().add(returnStatement);

    return block;
  }
}
